/*    
 * Copyright (c) 2013 ISoftStone, Inc.
 * All right reserved.
 *
 * 文件名：      ChargingItemServiceImpl.java
 * 作者:     Jacky Wang
 * 创建日期： 2013-7-25 上午10:32:51
 * 版本：           
 *
 */
package com.isoftstone.industry.aaa.core.service.assist.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.isoftstone.industry.persistance.bean.BillingCycle;
import com.isoftstone.industry.persistance.bean.ChargingItem;
import com.isoftstone.industry.persistance.bean.ChargingItemDetail;
import com.isoftstone.industry.aaa.core.dao.assist.ChargingItemDao;
import com.isoftstone.industry.aaa.core.dao.assist.ChargingItemDetailDao;
import com.isoftstone.industry.aaa.core.dao.assist.TicketRebuildTaskDao;
import com.isoftstone.industry.aaa.core.service.assist.IChargingItemService;
import com.isoftstone.modules.core.persistence.IGenericDao;
import com.isoftstone.modules.core.service.impl.GenericServiceImpl;
import com.isoftstone.modules.core.utils.DateUtils;

/**
 *
 * @author jacky
 */
@Service
public class ChargingItemServiceImpl extends GenericServiceImpl<ChargingItem, String> implements IChargingItemService
{
	@Resource
	private ChargingItemDao chargingItemDao;
	
	@Resource
	private ChargingItemDetailDao chargingItemDetailDao;
	
	@Resource
	private TicketRebuildTaskDao ticketRebuildTaskDao;
	
	@Override
	public IGenericDao<ChargingItem, String> getDao()
	{
		return chargingItemDao;
	}

	@Override
	public List<ChargingItem> findNeedChargeItemsByUser(String userId, Timestamp date)
	{
		return chargingItemDao.findEfficient(userId, ChargingItem.Status.VALID, date);
	}

	@Override
	public void changeLastChargingTime(String id, Timestamp modifiedTime)
	{
		chargingItemDao.changeLastChargingTime(id, modifiedTime);
	}

	@Override
	public void markInvalid(String id)
	{
		Timestamp now = DateUtils.getCurrentTimestamp();
		chargingItemDao.changeStatus(id, ChargingItem.Status.INVALID, now);
		chargingItemDetailDao.changeStatusByParent(id, ChargingItemDetail.Status.INVALID, now);
		ticketRebuildTaskDao.deleteByChargingItem(id,true, now);
	}

	@Override
	public long countNeedChargeItemsByUser(String userId, Timestamp date)
	{
		return chargingItemDao.countByUserAndStatusAndLastChargingTimeLessThan(userId, ChargingItem.Status.VALID, date);
	}

	@Override
	public List<ChargingItem> findNeedChargeItemsByUserAndBillingCycle(String userId, BillingCycle billingCycle)
	{
		return chargingItemDao.findEfficientInBillingCycle(userId, ChargingItem.Status.VALID, billingCycle.getBeginDate(), billingCycle.getEndDate());
	}

	@Override
	public long countNeedChargeItemsByUserInBillingCycle(String userId,
			BillingCycle billingCycle) {
		Timestamp date = DateUtils.getCurrentMonthFirstDay();
		return chargingItemDao.countByUserAndStatusAndLastChargingTimeAndEffDateLessThan(userId, ChargingItem.Status.VALID, date, billingCycle.getEndDate());
	}

}
